package com.example.day01_weather.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.day01_weather.R;
import com.example.day01_weather.bean.Station_iaqiBean;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 */
public class IaqiAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private ArrayList<Station_iaqiBean> list;
    private Context context;
    private int VIEW_TYPE_ONE = 1;
    private int VIEW_TYPE_TWO = 2;
    private final LayoutInflater inflater;

    public IaqiAdapter(ArrayList<Station_iaqiBean> list, Context context) {
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return VIEW_TYPE_ONE;
        } else {
            return VIEW_TYPE_TWO;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ONE) {
            View view = inflater.inflate(R.layout.item_iaqi_tab, parent, false);
            return new ViewHolderOne(view);
        } else {
            View view = inflater.inflate(R.layout.item_iaqi_list, parent, false);
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof ViewHolder){
            Station_iaqiBean.DataBean dataBean = list.get(position-1).getData();
            ((ViewHolder) holder).tvCity.setText(dataBean.getStation());
            ((ViewHolder) holder).tvDj.setText("1");
            ((ViewHolder) holder).tvPm.setText(dataBean.getPM2_5());
            ((ViewHolder) holder).tvZl.setText(dataBean.getQuality());
        }
    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    static

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_dj)
        TextView tvDj;
        @BindView(R.id.tv_city)
        TextView tvCity;
        @BindView(R.id.tv_pm)
        TextView tvPm;
        @BindView(R.id.tv_zl)
        TextView tvZl;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    static
    class ViewHolderOne extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_dj)
        TextView tvDj;
        @BindView(R.id.tv_city)
        TextView tvCity;
        @BindView(R.id.tv_pm)
        TextView tvPm;
        @BindView(R.id.tv_zl)
        TextView tvZl;

        ViewHolderOne(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
